from typing import List


class Solution:
    def specialArray(self, nums: List[int]) -> int:
        nums.sort()
        ans, left, right = -1, 0, len(nums) - 1
        while left <= right:
            mid = (left + right) // 2
            cnt = len(nums) - mid
            if nums[mid] >= cnt:
                ans = mid
                right = mid - 1
            else:
                left = mid + 1

        return -1 if ans == -1 or nums[ans - 1] == len(nums) - ans else len(nums) - ans
